package com.du.method;

import com.du.config.MysqlRule;
import com.du.entity.ConvertParam;
import com.du.entity.RenderSelectClz;
import com.du.enums.RenderSelectItem;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

/**
 * @Desc :
 * @Author: shuangdu
 * @date : 2021/3/26
 */
public class Window extends JFrame implements ActionListener, ClipboardOwner {

    private boolean loading = false;

    private JRadioButton radio1 = new JRadioButton("转为markdown", true);
    private JRadioButton radio2 = new JRadioButton("转为java bean");
    private JLabel templateLabel = new JLabel("转换格式");
    private JTextField templateField = new JTextField(MysqlRule.DEFAULT_TEMPLATE_RULE, 25);
    private JButton templateIntroButton = new JButton("介绍");
    private JTextArea sourceField;
    private JTextArea targetField;
    private JButton submitButton;
    private JButton copyButton;
    private java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

    private JComboBox<RenderSelectClz> renderSelect = new JComboBox<>();

    private void setRenderSelect() {
        Arrays.stream(RenderSelectItem.values()).forEach(i -> renderSelect.addItem(RenderSelectClz.fromEnum(i)));
    }

    public void addPanel() {
        // before
        setRenderSelect();

        // top
        JPanel topPanel = new JPanel();
        templateField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        topPanel.add(templateLabel);
        topPanel.add(templateField);
        topPanel.add(templateIntroButton);
        templateIntroButton.addActionListener(this);
//        ButtonGroup group=new ButtonGroup();
//        group.add(radio1);
//        group.add(radio2);
//        topPanel.add(radio1);
//        topPanel.add(radio2);
        topPanel.add(renderSelect);
        add(topPanel, BorderLayout.NORTH);

        // source
        sourceField = new JTextArea(18, 35);
        sourceField.setTabSize(2);
        sourceField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        sourceField.setLineWrap(true);
        sourceField.setWrapStyleWord(true);
        JScrollPane textPanel = new JScrollPane(sourceField);

        JPanel westPanel = new JPanel();
        westPanel.add(textPanel);
        add(westPanel,BorderLayout.WEST);


        // target
        targetField = new JTextArea(18, 35);
        targetField.setTabSize(2);

        targetField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        targetField.setLineWrap(true);
        targetField.setWrapStyleWord(true);

        targetField.setEditable(false);
        JScrollPane targetPanel = new JScrollPane(targetField);

        JPanel eastPanel = new JPanel();
        eastPanel.add(targetPanel);
        add(eastPanel,BorderLayout.EAST);


        // button
        JPanel bottom = new JPanel();
        submitButton = new JButton("开始转换");
        submitButton.addActionListener(this);
        copyButton = new JButton("复制结果");
        copyButton.addActionListener(this);

        bottom.add(submitButton);
        bottom.add(copyButton);
        add(bottom, BorderLayout.SOUTH);
    }


    public void actionPerformed(ActionEvent e) {
        if (this.loading) {
            JOptionPane.showMessageDialog(this, "任务正在执行中...");
            return;
        }
        if (e.getSource() == submitButton) {
            // 执行转换
            this.loading = true;

            RenderSelectClz clz = (RenderSelectClz)renderSelect.getSelectedItem();
            if (Objects.isNull(clz)) {
                return;
            }
            RenderSelectItem item = clz.getE();
            if (Objects.isNull(item)) {
                JOptionPane.showMessageDialog(this, "未找到匹配的类型!");
                return;
            }

            System.out.println("转换类型：" + clz.toString());
            String text = sourceField.getText();
            String template = templateField.getText();
            if (template == null || "".equals(template)) {
                template = MysqlRule.DEFAULT_TEMPLATE_RULE;
            }

            ConvertParam param = ConvertContext.holder.getParam();
            param.setTemplateText(template)
                    .setSourceText(sourceField.getText())
                    .setRenderSelectItem(item);

            String target = Convert.sourceParse();

            System.out.println("===>.oO转换结果Oo.<===");
            System.out.println(target);
            targetField.setText(target);

            this.loading = false;
            System.out.println("===>转换完成<===");
        } else if (e.getSource() == copyButton) {
            // 复制结果
            String text = targetField.getText();
            if (text == null || "".equals(text)) return;
            StringSelection val = new StringSelection(targetField.getText());
            clipboard.setContents(val, this);
            JOptionPane.showMessageDialog(this, "复制成功!");
        } else if (e.getSource() == templateIntroButton) {
            // 模板映射介绍
            Map<String, String> colMap = MysqlRule.COL_MAP;
            StringBuilder sb = new StringBuilder();
            sb.append("例如: |@1|@2|@3|@4| \n");
            Set<String> keySet = colMap.keySet();
            for (String nextKey : keySet) {
                sb.append(nextKey).append(": ").append(colMap.get(nextKey)).append("\n");
            }

            sb.append("注意：转java bean不需要设置格式!");
            JOptionPane.showMessageDialog(this, sb.toString());
        }
    }

    public static void main(String []args){
        Window win = new Window();

        win.setTitle("Title");

        win.setSize(1000, 500);

        // 默认居中
        win.setLocationRelativeTo(null);

        Container con = win.getContentPane();
//        con.setBackground(Color.BLUE);

        win.addPanel();

        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        win.setVisible(true);
    }

    public void lostOwnership(Clipboard clipboard, Transferable contents) {

    }
}
